//
//  DDContactReplyCell.m
//  DingDongZP
//
//  Created by Ray on 16/4/26.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDContactReplyCell.h"

@interface DDContactReplyCell ()
@property (nonatomic, strong) UILabel *infoLab;
@end

@implementation DDContactReplyCell
{
    UIView *inner;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {

        inner = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 100)];
        inner.backgroundColor = [UIColor whiteColor];
        inner.layer.masksToBounds = YES;
        inner.layer.cornerRadius = 10.0;
        [self.messageContentView addSubview:inner];
        
        UILabel *infoLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, CGRectGetWidth(inner.bounds), 40)];
        infoLab.textAlignment = NSTextAlignmentCenter;
        
        infoLab.font = [UIFont systemFontOfSize:17];
        infoLab.textColor = COLOR_FONT_BLACK;
        
        infoLab.numberOfLines = 0;
        [inner addSubview:infoLab];
        
        UIView *sep1 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(infoLab.frame)+10, 250, 0.5)];
        sep1.backgroundColor = [UIColor lightGrayColor];
        [inner addSubview:sep1];
        
        UIView *sep2 = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetWidth(inner.bounds)/2,CGRectGetMaxY(sep1.frame), 0.5, 40)];
        sep2.backgroundColor = [UIColor lightGrayColor];
        [inner addSubview:sep2];
        
        UIButton *accept = [UIButton setDetailsBtn:@"同意" picName:nil btnBackColor:nil titleColor:[UIColor colorWithRed:0.188 green:0.682 blue:0.678 alpha:1.000]];
        accept.tag = 10;
        accept.frame = CGRectMake(0,CGRectGetMaxY(sep1.frame), CGRectGetWidth(inner.bounds)/2, 40);
        [inner addSubview:accept];
        [accept addTarget:self action:@selector(actionAccept:) forControlEvents:UIControlEventTouchUpInside];
        
        UIButton *refuse = [UIButton setDetailsBtn:@"拒绝" picName:nil btnBackColor:nil titleColor:[UIColor colorWithRed:1.000 green:0.255 blue:0.255 alpha:1.000]];
        refuse.tag = 11;
        refuse.frame = CGRectMake(CGRectGetWidth(inner.bounds)/2, CGRectGetMaxY(sep1.frame),CGRectGetWidth(inner.bounds)/2, 40);
        [inner addSubview:refuse];
        [refuse addTarget:self action:@selector(actionRefuse:) forControlEvents:UIControlEventTouchUpInside];
        
        self.infoLab = infoLab;
        
        self.messageTimeLabel.backgroundColor = [UIColor clearColor];
        self.messageTimeLabel.textColor = COLOR_FONT_DARK_GRAY;
    }
    return self;
}

- (void)setDataModel:(RCMessageModel *)model
{
    [super setDataModel:model];

    DDContactRequestMessage *message = (DDContactRequestMessage *)model.content;
    
    NSUserDefaults *hadChange = [NSUserDefaults standardUserDefaults];
    
    
    if (message.changeTime) {
        
        if ([hadChange boolForKey:message.changeTime] == YES) {
            for (int i = 10; i<12; i++) {
                UIButton *clickButton = (UIButton *)[inner viewWithTag:i];
                [clickButton setTitleColor:COLOR_FONT_LIGHT_GRAY forState:UIControlStateNormal];
                [clickButton setUserInteractionEnabled:NO];
            }
        }else{
            
            UIButton *sureButton = (UIButton *)[inner viewWithTag:10];
            [sureButton setTitleColor:[UIColor colorWithRed:0.188 green:0.682 blue:0.678 alpha:1.000] forState:UIControlStateNormal];
            [sureButton setUserInteractionEnabled:YES];
            
            UIButton *refuseButton = (UIButton *)[inner viewWithTag:11];
            [refuseButton setTitleColor:[UIColor colorWithRed:1.000 green:0.255 blue:0.255 alpha:1.000] forState:UIControlStateNormal];
            [refuseButton setUserInteractionEnabled:YES];
        }

    }else{
    
            UIButton *sureButton = (UIButton *)[inner viewWithTag:10];
            [sureButton setTitleColor:[UIColor colorWithRed:0.188 green:0.682 blue:0.678 alpha:1.000] forState:UIControlStateNormal];
            [sureButton setUserInteractionEnabled:YES];
       
            UIButton *refuseButton = (UIButton *)[inner viewWithTag:11];
            [refuseButton setTitleColor:[UIColor colorWithRed:1.000 green:0.255 blue:0.255 alpha:1.000] forState:UIControlStateNormal];
            [refuseButton setUserInteractionEnabled:YES];
    }
    
    if (message.requestType == DDContactRequestPhone) {
        
        self.infoLab.text = @"和您交换电话，您是否同意？";
        
    } else {
        
        self.infoLab.text = @"和您交换微信，您是否同意？";
    }
}

- (void)actionAccept:(id)sender {
    if (self.contactDelegate) {
        DDContactRequestMessage *repmodel = (DDContactRequestMessage *)self.model.content;
        [self.contactDelegate dealWithContactRequestMessage:repmodel accept:YES];
        [self didnotClick];
    }
}

- (void)actionRefuse:(id)sender {
    if (self.contactDelegate) {
        DDContactRequestMessage *repmodel = (DDContactRequestMessage *)self.model.content;
        [self.contactDelegate dealWithContactRequestMessage:repmodel accept:NO];
        [self didnotClick];
    }
}

// 点击后 两个按钮变灰色，且均不可点击
- (void)didnotClick
{
    for (int i = 10; i<12; i++) {
        UIButton *clickButton = (UIButton *)[inner viewWithTag:i];
        [clickButton setTitleColor:COLOR_FONT_LIGHT_GRAY forState:UIControlStateNormal];
        [clickButton setUserInteractionEnabled:NO];
    }
}

@end
